package com.yngrp.controller.wo;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.ExceptionUtil;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.demo.entity.test.CourseEntity;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.yngrp.entity.Group2CustomerNo;
import com.yngrp.entity.GroupInfo;
import com.yngrp.entity.TaskCustomerNoDetail;
import com.yngrp.entity.TaskElecQuery;
import com.yngrp.entity.TaskGroupDetail;
import com.yngrp.service.wo.GroupPowerRateService;
import com.yngrp.util.Constant;

/**
 * @Description: TODO(集团电费查询处理类)
 * @author  SLH
 * @date  20160809 
 */
@Controller
@RequestMapping("/groupPRController")
public class GroupPowerRateController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(WorkOrderController.class);
	
	private SystemService systemService;
	
	private GroupPowerRateService groupRateService;
	
	@Autowired
	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}
	
	@Autowired
	public void setGroupRateService(GroupPowerRateService groupRateService) {
		this.groupRateService = groupRateService;
	}

	/**
	 *  集团列表页面跳转
	 * 
	 *  @return
	 */
	@RequestMapping(params = "groupListR")
	public String groupListR(HttpServletRequest request) {
		
		return "com/yngrp/group/groupList";
	}
	
	/***
	 * 集团信息列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param gi
	 */
	@RequestMapping(params = "groupGrid")
	public void groupGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, GroupInfo gi) {
		CriteriaQuery cq = new CriteriaQuery(GroupInfo.class, dataGrid);

		//集团名称
        String groupName = gi.getGroupName();
        if(StringUtils.isNotEmpty(groupName)) {
        	groupName = groupName.trim();
        	if(groupName.lastIndexOf("%")==-1){
        		groupName = "%"+groupName + "%";
            }
        	cq.like("groupName", groupName);
        }        
        //联系人
        String contactMan = gi.getContactMan();
        if(StringUtils.isNotEmpty(contactMan)) {
        	contactMan = contactMan.trim();
        	if(contactMan.lastIndexOf("%")==-1){
        		contactMan = "%"+contactMan + "%";
            }
        	cq.like("contactMan", contactMan);
        }
        //联系电话
        String contactPhone = gi.getContactPhone();
        if(StringUtils.isNotEmpty(contactPhone)) {
        	contactPhone = contactPhone.trim();
        	if(contactPhone.lastIndexOf("%")==-1){
        		contactPhone = "%"+contactPhone + "%";
            }
        	cq.like("contactPhone", contactPhone);
        }
        //集团邮箱
        String contactEmail = gi.getContactEmail();
        if(StringUtils.isNotEmpty(contactEmail)) {
        	contactEmail = contactEmail.trim();
        	if(contactEmail.lastIndexOf("%")==-1){
        		contactEmail = "%"+contactEmail + "%";
            }
        	cq.like("contactEmail", contactEmail);
        }
        
        //只查询本部门的集团信息
        TSUser user = ResourceUtil.getSessionUserName();
        cq.eq("createDepartmentid", user.getCurrentDepart().getId());
        	
         
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 添加集团页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "aouGroup")
	public ModelAndView aouGroup(GroupInfo group, HttpServletRequest req) {
		if (group.getId() != null) {
			group = systemService.getEntity(GroupInfo.class, group.getId());
			req.setAttribute("group", group);
		}
		
		return new ModelAndView("com/yngrp/group/addGroup");
	}

	
	/**
	 * 编辑保存集团
	 *
	 * @param group
	 * @return
	 */
	@RequestMapping(params = "saveGroup")
	@ResponseBody
	public AjaxJson saveGroup(GroupInfo group, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(group.getId())) {
			groupRateService.saveGroup(group);
			
			message = "更新集团用户【"+group.getGroupName()+"】成功";
			systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_UPDATE);
		} else {			
			groupRateService.saveGroup(group);
			
			message = "添加集团用户【"+group.getGroupName()+"】成功";
			systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_INSERT);
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 删除集团
	 *
	 * @return
	 */
	@RequestMapping(params = "delGroup")
	@ResponseBody
	public AjaxJson delGroup(GroupInfo group, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		List<Object> listGC = systemService.findHql("from Group2CustomerNo where groupInfo.id=?", group.getId());
		if(listGC!=null && listGC.size()>0){
			message = "请先删除集团下的所有户号";
			j.setMsg(message);
			return j;
		}		
		group = systemService.getEntity(GroupInfo.class, group.getId());
		message = "删除集团用户【"+group.getGroupName()+"】成功";
		systemService.delete(group);
		
		systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_DEL);
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 检查集团激活状态
	 *
	 * @return
	 */
	@RequestMapping(params = "checkGroupActive")
	@ResponseBody
	public AjaxJson checkGroupActive(HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		String groupid = request.getParameter("groupid");
		GroupInfo group = systemService.getEntity(GroupInfo.class, Integer.parseInt(groupid));
		message = group.getActiveState();
		j.setSuccess(true);
		j.setMsg(message);
		return j;
	}
	
	/**
     * 集团户号维护页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goCustomerNoGrid")
	public ModelAndView goCustomerNoGrid(HttpServletRequest request) {
		String groupid = request.getParameter("groupid");
		List<Map<String,Object>> queryModeCount= systemService.findForJdbc("SELECT querymode_desc,COUNT(*) cnt FROM yl_group_customerno WHERE group_id=?  GROUP BY querymode_desc", groupid);
	     
		request.setAttribute("queryModeCount", queryModeCount);  
        request.setAttribute("groupid", groupid);  
		return new ModelAndView("com/yngrp/group/customerNoList");
	}
	
	/***
	 * 集团户号列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param gc
	 */
	@RequestMapping(params = "customerNoGrid")
	public void customerNoGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, Group2CustomerNo gc) {
		CriteriaQuery cq = new CriteriaQuery(Group2CustomerNo.class, dataGrid);
		
		//字段选项
		String pmd = request.getParameter("pmd");
		//字段值
		String pmv = request.getParameter("pmv");
		//电费查询方式
		String queryMode = request.getParameter("queryMode");
		
        if(StringUtils.isNotEmpty(pmv)) {
        	pmv = pmv.trim();
        	if(pmd.equals("id")){
        		cq.like(pmd, Integer.parseInt(pmv));
        	}else{
        		if(pmv.lastIndexOf("%")==-1){
            		pmv = "%"+ pmv + "%";
                }
        		cq.like(pmd, pmv);
        	}
        }
        
        if(StringUtils.isNotEmpty(queryMode)) {
        	cq.eq("queryMode", queryMode);
        }
		
		//集团ID
        String groupid = request.getParameter("groupid");
        if(StringUtils.isNotEmpty(groupid)) {
        	 
        	cq.eq("groupInfo.id", Integer.parseInt(groupid));
        }
 
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 集团户号添加页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "aouCustomerNo")
	public ModelAndView aouCustomerNo(HttpServletRequest request,Group2CustomerNo gc) {	
		if (gc.getId() != null) {
			gc = systemService.getEntity(Group2CustomerNo.class, gc.getId());
			request.setAttribute("customerObj", gc);
		}
		String groupid = request.getParameter("groupid");
		request.setAttribute("groupid", groupid);
		
		return new ModelAndView("com/yngrp/group/addCustomerNo");
	}
	
	/**
	 * 编辑保存集团户号
	 *
	 * @param gc
	 * @return
	 */
	@RequestMapping(params = "saveCustomerNo")
	@ResponseBody
	public AjaxJson saveCustomerNo(Group2CustomerNo gc, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		
		GroupInfo groupInfo = systemService.getEntity(GroupInfo.class, gc.getGroupInfo().getId());
		//如果户号电费方式与集团不一致 ，并且集团查询没有设定为【每月】
		if(!groupInfo.getQueryMode().equals(gc.getQueryMode()) && !groupInfo.getQueryMode().equals(Constant.EVERY_MONTH)){
			
			message = "户号电费查询方式与集团设定的查询方式不匹配!";
			j.setMsg(message);
			return j;
		}
		
		
		if (StringUtil.isNotEmpty(gc.getId())) {
			groupRateService.saveCustomerNo(gc);
			message = "更新户号【"+gc.getCustomerNo()+"】成功";
			
			systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_UPDATE);
		} else {
			groupRateService.saveCustomerNo(gc);
			
			message = "添加户号【"+gc.getCustomerNo()+"】成功";
			systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_INSERT);
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 删除集团户号
	 *
	 * @return
	 */
	@RequestMapping(params = "delCustomerNo")
	@ResponseBody
	public AjaxJson delCustomerNo(Group2CustomerNo gc, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		gc = systemService.getEntity(Group2CustomerNo.class, gc.getId());
		
		message = "删除户号【"+gc.getCustomerNo()+"】成功";
		systemService.delete(gc);
		systemService.addLog(message, Globals.Log_Leavel_INFO, Globals.Log_Type_DEL);
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 批量删除集团户号
	 *
	 * @return
	 */
	@RequestMapping(params = "removeMoreCustomerNo")
	@ResponseBody
	public AjaxJson removeMoreCustomerNo(HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();

		String id = "";
		Group2CustomerNo gc = null;
		String ids = request.getParameter("ids");
		String[] idArr = ids.split(",");
		for(int i=0;i<idArr.length;i++){
		    id = idArr[i];
			if(StringUtils.isNotBlank(id)){
				gc = systemService.getEntity(Group2CustomerNo.class, id);
				systemService.delete(gc);
				systemService.addLog(gc.getCustomerNo()+"户号删除成功", Globals.Log_Leavel_INFO, Globals.Log_Type_DEL);
			}
		}
		message = "批量删除完成";
		j.setMsg(message);
		return j;
	}
	
	/**
     * 电费查询任务页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goTaskList")
	public ModelAndView goTaskList(HttpServletRequest request) {

		return new ModelAndView("com/yngrp/group/taskList");
	}
	
	
	/***
	 * 欠费查询申请列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param aqa
	 */
	@RequestMapping(params = "toDoTaskGrid")
	public void toDoTaskGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, TaskElecQuery teq) {
		CriteriaQuery cq = new CriteriaQuery(TaskElecQuery.class, dataGrid);

		 
		//只取未完成的任务
		cq.eq("state", Constant.AP_0_STATE);
 
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
     * 任务集团明细页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goTaskGroupList")
	public ModelAndView goTaskGroupList(HttpServletRequest request) {
		//任务编号
		String task_id = request.getParameter("id");
		 if(StringUtils.isNotEmpty(task_id)) {
			TaskElecQuery teg = systemService.getEntity(TaskElecQuery.class, Integer.parseInt(task_id));
			request.setAttribute("taskObj", teg);
		}
		

		return new ModelAndView("com/yngrp/group/arrearsGroupDetail");
	}
	
	/***
	 * 任务集团明细列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param aqa
	 */
	@RequestMapping(params = "taskGroupGrid")
	public void taskGroupGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, TaskGroupDetail tgd) {
		CriteriaQuery cq = new CriteriaQuery(TaskGroupDetail.class, dataGrid);
		cq.createAlias("queryTask","queryTask");
		cq.createAlias("groupInfo","groupInfo");
		
		//任务ID
        String task_id = request.getParameter("task_id");
        if(StringUtils.isNotEmpty(task_id)) {
        	 
        	cq.eq("queryTask.id", Integer.parseInt(task_id));
        }
        
        //集团名称
        String groupName = request.getParameter("groupInfo.groupName");
        if(StringUtils.isNotEmpty(groupName)) {
        	groupName = groupName.trim();
        	if(groupName.lastIndexOf("%")==-1){
        		groupName = "%"+groupName + "%";
            }
        	cq.like("groupInfo.groupName", groupName);
        }
		  
 
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 完成任务
	 *
	 * @return
	 */
	@RequestMapping(params = "finishTask")
	@ResponseBody
	public AjaxJson finishTask(TaskElecQuery teq, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		groupRateService.finishTask(teq);
		
		message = "任务已完成";
		systemService.addLog(teq.getId()+message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		j.setMsg(message);
		return j;
	}
	
	/**
     * 集团户号明细页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goTaskCustomerNoList")
	public ModelAndView goTaskCustomerNoList(HttpServletRequest request) {
		//任务集团户号明细编号
		String id = request.getParameter("id");
		 if(StringUtils.isNotEmpty(id)) {
			 TaskGroupDetail tgd = systemService.getEntity(TaskGroupDetail.class, id);			 
			 
			 request.setAttribute("tgdObj", tgd);
		}
		

		return new ModelAndView("com/yngrp/group/arrearsCustomerNoDetail");
	}
	
	/***
	 * 集团户号明细列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param tcnd
	 */
	@RequestMapping(params = "taskCustomerNoGrid")
	public void taskCustomerNoGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, TaskCustomerNoDetail tcnd) {
		CriteriaQuery cq = new CriteriaQuery(TaskCustomerNoDetail.class, dataGrid);
		cq.createAlias("queryTask","queryTask");
		cq.createAlias("groupInfo","groupInfo");
		
		//任务ID
        String task_id = request.getParameter("task_id");
        if(StringUtils.isNotEmpty(task_id)) {
        	 
        	cq.eq("queryTask.id", Integer.parseInt(task_id));
        }
        
        //集团ID
        String group_id = request.getParameter("group_id");
        if(StringUtils.isNotEmpty(group_id)) {
        	 
        	cq.eq("groupInfo.id", Integer.parseInt(group_id));
        }
        
        //集团户号
        String customerNo = request.getParameter("customerNo");
        if(StringUtils.isNotEmpty(customerNo)) {
        	customerNo = customerNo.trim();
        	if(customerNo.lastIndexOf("%")==-1){
        		customerNo = "%"+customerNo + "%";
            }
        	cq.like("customerNo", customerNo);
        }
		  
 
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 删除查询任务
	 *
	 * @return
	 */
	@RequestMapping(params = "delTask")
	@ResponseBody
	public AjaxJson delTask(HttpServletRequest request) {
		String id = request.getParameter("id");
		AjaxJson j = new AjaxJson();
		String message = null;
		TaskElecQuery teq =  systemService.getEntity(TaskElecQuery.class, Integer.parseInt(id));
		if(teq.getState().equals(Constant.AP_0_STATE)){
			groupRateService.deleteQueryTask(teq);
			message = "删除成功";
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}else{
			message = "已完成的任务不能删除！";
		}
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 删除查询任务集团信息
	 *
	 * @return
	 */
	@RequestMapping(params = "delTaskGroup")
	@ResponseBody
	public AjaxJson delTaskGroup(HttpServletRequest request) {
		String id = request.getParameter("id");
		AjaxJson j = new AjaxJson();
		String message = null;
		TaskGroupDetail teq =  systemService.getEntity(TaskGroupDetail.class, id);
		groupRateService.deleteTaskGroup(teq);
		message = "删除成功";
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}
	
	/**
     * 任务综合页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goSnyQueryTaskList")
	public ModelAndView goSnyQueryTaskList(HttpServletRequest request) {

		return new ModelAndView("com/yngrp/group/synQueryTaskList");
	}
	
	/***
	 * 查询任务综合查询
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param aqa
	 */
	@RequestMapping(params = "snyQueryTaskGrid")
	public void snyQueryTaskGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid, TaskGroupDetail tgd) {
		CriteriaQuery cq = new CriteriaQuery(TaskGroupDetail.class, dataGrid);
		cq.createAlias("queryTask","queryTask");
		cq.createAlias("groupInfo","groupInfo");
		
		//任务状态为已完成
		cq.eq("queryTask.state", Constant.AP_1_STATE);
        
        //任务标题
        String title = request.getParameter("queryTask.title");
        if(StringUtils.isNotEmpty(title)) {
        	title = title.trim();
        	if(title.lastIndexOf("%")==-1){
        		title = "%"+title + "%";
            }
        	cq.like("queryTask.title", title);
        }
        
        //集团名称
        String groupName = request.getParameter("groupInfo.groupName");
        if(StringUtils.isNotEmpty(groupName)) {
        	groupName = groupName.trim();
        	if(groupName.lastIndexOf("%")==-1){
        		groupName = "%"+groupName + "%";
            }
        	cq.like("groupInfo.groupName", groupName);
        }
		  
 
        cq.add();
        this.systemService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}
	
	/**
	 * 导出任务明细excel
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(TaskCustomerNoDetail tcnd,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
		String task_id = request.getParameter("task_id");
		
		TaskElecQuery teq = this.systemService.get(TaskElecQuery.class, Integer.parseInt(task_id));
		String taskTitle = teq.getQueryDate()+"电费查询任务";
	    
		CriteriaQuery cq = new CriteriaQuery(TaskCustomerNoDetail.class, dataGrid);
		cq.createAlias("queryTask","queryTask");
		cq.eq("queryTask.id",Integer.parseInt(task_id));

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, tcnd, request.getParameterMap());
		List<TaskCustomerNoDetail> tsGcns = this.groupRateService.getListByCriteriaQuery(cq,false);
		
		for(TaskCustomerNoDetail tsGcn:tsGcns){
			
			tsGcn.setContactEmail(tsGcn.getGroupInfo().getContactEmail());
			tsGcn.setQueryDate(teq.getQueryDate());
			tsGcn.setGroupName(tsGcn.getGroupInfo().getGroupName());
		}
		
		modelMap.put(NormalExcelConstants.FILE_NAME,taskTitle);
		modelMap.put(NormalExcelConstants.CLASS,TaskCustomerNoDetail.class);
		modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("电费查询任务", "导出人:"+ResourceUtil.getSessionUserName().getRealName(),
				"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST,tsGcns);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	
	/**
     * 手动生成任务页面跳转
     * @param request
     * @return
     */
	@RequestMapping(params = "goCreateTask")
	public ModelAndView goCreateTask(HttpServletRequest request) {
		String minDate = DateUtils.formatDate(new Date(), Constant.FORMART_QUERY_DAY);
		
		SimpleDateFormat format = new SimpleDateFormat( Constant.FORMART_QUERY_DAY); 
		Calendar cal_1=Calendar.getInstance();
		cal_1.add(Calendar.MONTH, 1);
		String maxDate = format.format(cal_1.getTime());
		
		request.setAttribute("minDate", minDate);
		request.setAttribute("maxDate", maxDate);
		return new ModelAndView("com/yngrp/group/addTask");
	}
	
	/**
	 * 手动生成任务
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "createQueryTask")
	@ResponseBody
	public AjaxJson createQueryTask(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String message = null;
		String queryStartDay = request.getParameter("queryStartDay");
		String queryEndDay = request.getParameter("queryEndDay");
		
		//if(groupRateService.checkQueryTask(queryDay)){
			//String rMsg = groupRateService.createQueryTask(queryDay, Constant.MANUAL_CREATE_MODE);
			String rMsg = groupRateService.createQueryTask(queryStartDay, queryEndDay,Constant.MANUAL_CREATE_MODE);
			if(rMsg.equals(Constant.SUCCESS)){
				j.setSuccess(true);
				message = "查询任务已生成";
			}else{
				j.setSuccess(false);
				message = "查询任务生成失败！";
			}
			
		//}else{
			//j.setSuccess(false);
			//message = queryDay+"查询任务已存在！";
		//}		
		j.setMsg(message);
		return j;
	}
	
	/**
	 * 导出集团用户号excel 使模板
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByCustomerNoT")
	public String exportXlsByCustomerNoT(Group2CustomerNo gcn,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("exportUserName", ResourceUtil.getSessionUserName().getRealName());
		
        modelMap.put(TemplateExcelConstants.FILE_NAME,"集团户号列表");
        modelMap.put(TemplateExcelConstants.PARAMS,new TemplateExportParams("export/template/groupCnoTmpl.xls",0));
        modelMap.put(TemplateExcelConstants.MAP_DATA,map);
        return TemplateExcelConstants.JEECG_TEMPLATE_EXCEL_VIEW;
        
	}
	
	/**
	 * 导入功能跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest request) {
		String groupid = request.getParameter("groupid"); 
		request.setAttribute("groupid", groupid);
		
		return new ModelAndView("com/yngrp/group/customerno_excel_upload");
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		String groupid = request.getParameter("groupid"); 
		GroupInfo groupInfo = systemService.get(GroupInfo.class, Integer.parseInt(groupid));
		
		//删除集团历史户号
		this.systemService.executeSql("delete from yl_group_customerno where group_id = ?", groupid);
		systemService.addLog("批量删除【"+groupInfo.getGroupName()+"】集团全部户号成功", Globals.Log_Leavel_INFO, Globals.Log_Type_DEL);
		
		AjaxJson j = new AjaxJson();

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				String queryMode = "";
				int n = 0;//成功上传数量
				int m = 0;//查询方式不正确数
				List<Group2CustomerNo> tsCustomers = ExcelImportUtil.importExcel(file.getInputStream(),Group2CustomerNo.class,params);
				
				//验证上传户号查询方式正确性
				for (Group2CustomerNo gcn : tsCustomers) {
					queryMode = groupRateService.getCodeByName(Constant.AP_QUERYMODE,gcn.getQueryModeDesc());
				    gcn.setQueryMode(queryMode);
					//如果户号电费方式与集团不一致 ，并且集团查询没有设定为【每月】
					if(!groupInfo.getQueryMode().equals(gcn.getQueryMode()) && !groupInfo.getQueryMode().equals(Constant.EVERY_MONTH)){						
						 m++;
					}
				}
				
				if(m>0){
					String message = "有"+m+"个户号电费查询方式与集团设定的查询方式不匹配，导入失败!";
					j.setMsg(message);
					return j;
				}
				
				for (Group2CustomerNo gcn : tsCustomers) {					
					gcn.setGroupInfo(groupInfo);
					systemService.save(gcn);
					n = n+1;					
				}

			    j.setMsg("导入成功"+n+"户！");					
			    systemService.addLog("批量上传【"+groupInfo.getGroupName()+"】集团"+n+"个户号成功", Globals.Log_Leavel_INFO, Globals.Log_Type_UPLOAD);
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				systemService.addLog("批量上传【"+groupInfo.getGroupName()+"】集团户号失败："+ExceptionUtil.getExceptionMessage(e), Globals.Log_Leavel_ERROR, Globals.Log_Type_UPLOAD);
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}
	
	/**
	 * 导出集团户号明细excel
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportCustomerNoXls")
	public String exportCustomerNoXls(Group2CustomerNo gcn,HttpServletRequest request,HttpServletResponse response
			, DataGrid dataGrid,ModelMap modelMap) {
		String groupid = request.getParameter("groupid");
		
		GroupInfo group = this.systemService.get(GroupInfo.class, Integer.parseInt(groupid));
		String taskTitle = group.getGroupName()+"集团户号列表";
	    
		CriteriaQuery cq = new CriteriaQuery(Group2CustomerNo.class, dataGrid);
		//cq.createAlias("queryTask","queryTask");
		cq.eq("groupInfo.id",Integer.parseInt(groupid));

		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, gcn, request.getParameterMap());
		List<TaskCustomerNoDetail> tsGcns = this.groupRateService.getListByCriteriaQuery(cq,false);
		
		
		modelMap.put(NormalExcelConstants.FILE_NAME,taskTitle);
		modelMap.put(NormalExcelConstants.CLASS,Group2CustomerNo.class);
		modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("集团户号列表", "导出人:"+ResourceUtil.getSessionUserName().getRealName(),
				"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST,tsGcns);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
}
